#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <string.h>
#include <winsock2.h>  // Use winsock2.h on Windows
#include <windows.h>


#define CONFIG_FILE  "./config.ini"
typedef signed short int16_t;

int BUFFER_SIZE;

typedef struct Config{
    char ip[16];
    int port;
    int chcnt;
    int snapshots;
    
    void coutConfig()
    {
    	std::cout<<"ip:"<<this->ip<<std::endl;
		std::cout<<"port:"<<this->port<<std::endl;
        std::cout<<"chcnt:"<<this->chcnt<<std::endl;
        std::cout<<"snapshots:"<<this->snapshots<<std::endl;
	}

} Config;

Config read_config(const char *section) {
    Config config;
    char strs[16] = "";
    DWORD chars_copied = 0;
    

    // Read the IP address
    chars_copied = GetPrivateProfileStringA(section, "IP", "", strs, sizeof(strs), CONFIG_FILE);
    if (chars_copied > 0) {
        strcpy(config.ip, strs);
    } else {
        printf("Failed to read IP from section '%s'.\n", section);
    }

    // Read the port number
    chars_copied = GetPrivateProfileStringA(section, "PORT", "", strs, sizeof(strs), CONFIG_FILE);
    if (chars_copied > 0) {
        config.port = atoi(strs);
    } else {
        printf("Failed to read Port from section '%s'.\n", section);
    }

    chars_copied = GetPrivateProfileStringA(section, "CHCNT", "", strs, sizeof(strs), CONFIG_FILE);
    if (chars_copied > 0) {
        config.chcnt = atoi(strs);
    } else {
        printf("Failed to read CHCNT from section '%s'.\n", section);
    }

    chars_copied = GetPrivateProfileStringA(section, "SNAPSHOTS", "", strs, sizeof(strs), CONFIG_FILE);
    if (chars_copied > 0) {
        config.snapshots = atoi(strs);
    } else {
        printf("Failed to read SNAPSHOTS from section '%s'.\n", section);
    }


	
    return config;
}




int main() {
	
	printf("\n----------------------------\n");
	Config config = read_config("config");

	config.coutConfig();
	BUFFER_SIZE = 2*config.chcnt*config.snapshots;
	
    // Initialize Winsock
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("Winsock initialization failed.\n");
        return 1;
    }



   // Create a socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET) {
        printf("Socket creation failed.\n");
        WSACleanup();
        return 1;
    }

    // Connect to the server
    SOCKADDR_IN serv_addr;
    int serv_addr_len = sizeof(serv_addr);
    //"10.21.232.156";//{'1','2','7','.','0','.','0','.','1'};  // Server IP address
    int result = WSAStringToAddress(config.ip, AF_INET, NULL, (SOCKADDR *)&serv_addr, &serv_addr_len);
    if (result != 0) {
        printf("Failed to convert IP address to SOCKADDR structure.\n");
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    serv_addr.sin_port = htons(config.port);

    if (connect(sock, (SOCKADDR *)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
        printf("Connection failed.\n");
        closesocket(sock);
        WSACleanup();
        return 1;
    }else
    {
    	int t=100;
    	while(t-->0)
    	{

            // Initialize your data
            int16_t data[6][1024];

            // Fill your data array
            for (int i = 0; i < 6; ++i) {
                for (int j = 0; j < 1024; ++j) {
                    data[i][j] = i * 1024 + j + t;  // Example: Fill with some test values
                }
            }

            // Convert the data to a byte string
            char* buffer = (char*)malloc(BUFFER_SIZE);
            memcpy(buffer, &data, BUFFER_SIZE);


	    	// Send the data
		    if (send(sock, buffer, BUFFER_SIZE, 0) == SOCKET_ERROR) {
		        printf("Failed to send data.\n");
		        closesocket(sock);
		        WSACleanup();
		        return 1;
		    }
		    //Sleep(10);
		    std::cout<<t;
            free(buffer);
		}
        std::cout<<std::endl;

	}

    
    closesocket(sock);
    WSACleanup();  // Clean up Winsock

    return 0;
}
